
/**  
* @Title: HtmlUtil.java
* @Package com.utils.html
* @Description: 
* @author likai
* @date 2016年12月21日
* @version V1.0  
*/

package com.utils.html;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.utils.date.DateUtil;

/**
 * @ClassName: HtmlUtil
 * @Description:
 * @author likai
 * @date 2016年12月21日
 *
 */

public class HtmlUtil {

	// 生成html字符串
	public static String getHtmlString(String title, String content) {
		String html = "<!DOCTYPE html><html lang='en-US'><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'><meta charset='UTF-8'>"
				+ "<link rel='stylesheet' type='text/css' href='http://tcsgarden.com:8080/SourceAccept/lovebaby/article.css'/></head><body><div class='wz-container'><div class='wz-content'><div class='wz-tit'>"
				+ title + "</div> " + "<div class='wz-data'><span class='wz-d-span'>"
				+ DateUtil.COMMON.getDateText(new Date()) + "</span></div><div class='wznr-content'>" + content
				+ "</div></div></body></html>";
		return html;
	}

	// 获取html内容
	public static String getContent(String htmlString) {
		return htmlString.substring(
				htmlString.indexOf("<div class='wznr-content'>") + "<div class='wznr-content'>".length(),
				htmlString.length() - "</div></div></body></html>".length());
	}

	// 获取html首个图片
	public static String getHtmlPic(String content) {
		Matcher m = Pattern.compile(" src=\"http://.*?/[0-9]+(.jpg|.png|.gif|.bmp|.jpeg)", Pattern.CASE_INSENSITIVE)
				.matcher(content);
		while (m.find()) {
			String match = m.group();
			match = match.substring(6, match.length()).replace(" ", "");
			return match;
		}
		return "";
	}

	// 获取html文件内容
	public static String getHtmlContent(File html) throws Exception {
		StringBuffer content = new StringBuffer();
		FileInputStream ism=null;
		InputStreamReader isr =null;
		BufferedReader bs =null;
		try {
			String info;
			ism = new FileInputStream(html);
			isr = new InputStreamReader(ism);
			bs = new BufferedReader(isr);
			info = bs.readLine();
			while (info != null) {
				content.append(info);
				info = bs.readLine();
			}
		} catch (IOException e) {
			
		}finally {
			if (ism!=null) {
				ism.close();
			}
			if (isr!=null) {
				isr.close();
			}
			if (bs!=null) {
				bs.close();
			}
		}
		return content.toString();
	}

	/**
	 * html字符串中字符替换(<,>,&,""替换)
	 * 
	 */
	public static String htmlEncode(String source) {
		if (source == null) {
			return "";
		}
		String html = "";
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < source.length(); i++) {
			char c = source.charAt(i);
			switch (c) {
			case '<':
				buffer.append("&lt;");
				break;
			case '>':
				buffer.append("&gt;");
				break;
			case '&':
				buffer.append("&amp;");
				break;
			case '"':
				buffer.append("&quot;");
				break;
			case 10:
			case 13:
				break;
			default:
				buffer.append(c);
			}
		}
		html = buffer.toString();
		return html;
	}

	/**
	 * html标签内容提取,去除html标签
	 */
	public static String filterHtml(String source) {
		Pattern pattern = Pattern.compile("<([^>]*)>");
		Matcher matcher = pattern.matcher(source);
		StringBuffer sb = new StringBuffer();
		boolean result1 = matcher.find();
		while (result1) {
			matcher.appendReplacement(sb, "");
			result1 = matcher.find();
		}
		matcher.appendTail(sb);
		return sb.toString();
	}

	public static void main(String[] args) {
		System.out.println(htmlEncode("<th style='width: 7%'>编号</th>"));
	}
}
